package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

import model.Animal;
import model.Field;
import model.Fox;
import model.Rabbit;
import model.Redfox;
import model.Simulator;

/**
 * Provide a graphical view of a rectangular field. This is 
 * a nested class (a class defined inside a class) which
 * defines a custom component for the user interface. This
 * component displays the field.
 * @author Team X
 * @version 1
 */
public class FieldView extends AbstractView
{
    private final int GRID_VIEW_SCALING_FACTOR = 6;
    private int gridWidth, gridHeight;
    private int xScale, yScale;
    Dimension size;
    private Graphics g;
    private Image fieldImage;

    /**
     * Create a new FieldView component.
     */
    public FieldView(Simulator simulator)
    {
    	super(simulator);
        gridHeight = Simulator.DEFAULT_DEPTH;
        gridWidth = Simulator.DEFAULT_WIDTH;
        size = new Dimension(0, 0);
    }

    /**
     * Tell the GUI manager how big we would like to be.
     */
    public Dimension getPreferredSize()
    {
        return new Dimension(gridWidth * GRID_VIEW_SCALING_FACTOR,
                             gridHeight * GRID_VIEW_SCALING_FACTOR);
    }
    
    /**
     * Prepare for a new round of painting. Since the component
     * may be resized, compute the scaling factor again.
     */
    public void preparePaint()
    {
        if(! size.equals(getSize())) {  // if the size has changed...
            size = getSize();
            fieldImage = this.createImage(size.width, size.height);
            g = fieldImage.getGraphics();

            xScale = size.width / gridWidth;
            if(xScale < 1) {
                xScale = GRID_VIEW_SCALING_FACTOR;
            }
            yScale = size.height / gridHeight;
            if(yScale < 1) {
                yScale = GRID_VIEW_SCALING_FACTOR;
            }
        }
    }
    
    /**
     * Paint on grid location on this field in a given color.
     */
    public void drawMark(int x, int y, Color color)
    {
        g.setColor(color);
        g.fillRect(x * xScale, y * yScale, xScale-1, yScale-1);
    }

    /**
     * The field view component needs to be redisplayed. Copy the
     * internal image to screen.
     */
    public void paintComponent(Graphics g)
    {
    	preparePaint();
    	createView();
    	
        if(fieldImage != null) 
        {
            Dimension currentSize = getSize();
            if(size.equals(currentSize)) 
            {
                g.drawImage(fieldImage, 0, 0, null);
            }
            else {
                // Rescale the previous image.
                g.drawImage(fieldImage, 0, 0, currentSize.width, currentSize.height, null);
            }
        }
    }

	@Override
	protected void createView() 
	{
        preparePaint();

        for(int row = 0; row < simulator.getField().getDepth(); row++) 
        {
            for(int col = 0; col < simulator.getField().getWidth(); col++) 
            {
                Object animal = simulator.getField().getObjectAt(row, col);
                
                if(animal != null) 
                {
                	Color color = simulator.getColor(animal.getClass());
                	
                	if(animal instanceof Rabbit || animal instanceof Fox || animal instanceof Redfox)
                	{ 
                		Animal animalBeing = (Animal) animal;
                		if(animalBeing.sick) color = Color.MAGENTA;
                	}
                	
                    drawMark(col, row, color);
                }else if(simulator.getField().getGrassHeight(row, col) > 0){
                    drawMark(col, row, Color.green);
                }
                else 
                {
                    drawMark(col, row, Simulator.EMPTY_COLOR);
                }
            }
        }
	}
    
    /**
     * Show the current status of the field.
     * @param step Which iteration step it is.
     * @param field The field whose status is to be displayed.
     */
    /*public void showStatus(int step, Field field)
    {
        if(!isVisible()) {
            setVisible(true);
        }
            
        stepLabel.setText(STEP_PREFIX + step);
        stats.reset();
        
        fieldView.preparePaint();

        for(int row = 0; row < field.getDepth(); row++) {
            for(int col = 0; col < field.getWidth(); col++) {
                Object animal = field.getObjectAt(row, col);
                if(animal != null) {
                    stats.incrementCount(animal.getClass());
                    drawMark(col, row, simulator.getColor(animal.getClass()));
                }
                else {
                    drawMark(col, row, Simulator.EMPTY_COLOR);
                }
            }
        }
        stats.countFinished();

        // Population overzicht refreshen
        population.removeAll();
        stats.getPopulationDetails2(simulator.getField(), population);
        
        population.repaint();
        
        contentPane.repaint();
        //population.setText(POPULATION_PREFIX + stats.getPopulationDetails(field));
        repaint();
    } */
}